package org.bai.miaoshaproject.controller;

import org.bai.miaoshaproject.common.Const;
import org.bai.miaoshaproject.controller.viewobject.ItemVO;
import org.bai.miaoshaproject.error.BusinessException;
import org.bai.miaoshaproject.response.CommonReturnType;
import org.bai.miaoshaproject.service.ItemService;
import org.bai.miaoshaproject.service.model.ItemModel;
import org.bai.miaoshaproject.service.model.PromoModel;
import org.joda.time.format.DateTimeFormat;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * ClassName ItemController
 *
 * @author Bai
 * @version 1.0
 * @date 2019/6/1 0001 下午 14:00
 **/
@Controller
@RequestMapping(value = "/item")
@CrossOrigin(allowCredentials = "true", origins = {"*"})
public class ItemController {

    private final ItemService itemService;

    @Autowired
    public ItemController(ItemService itemService) {
        this.itemService = itemService;
    }

    @PostMapping(value = "/create", consumes = {Const.CONTENT_TYPE_FORMED})
    public @ResponseBody CommonReturnType createItem(
            @RequestParam(name = "title") String title,
            @RequestParam(name = "price") BigDecimal price,
            @RequestParam(name = "stock") Integer stock,
            @RequestParam(name = "imgUrl") String imgUrl,
            @RequestParam(name = "description") String description ) throws BusinessException {

        //封装Service请求来创建对象
        ItemModel itemModel = new ItemModel();
        itemModel.setTitle(title);
        itemModel.setPrice(price);
        itemModel.setStock(stock);
        itemModel.setImgUrl(imgUrl);
        itemModel.setDescription(description);

        ItemModel itemModelFromReturn = itemService.createItem(itemModel);
        ItemVO itemVo = this.convertVOFromModel(itemModelFromReturn);
        return CommonReturnType.creatSuccess(itemVo);
    }

    @GetMapping(value = "/get")
    public @ResponseBody CommonReturnType getItem(@RequestParam(name = "id")Integer id){
        ItemModel itemModel = itemService.getItemById(id);
        ItemVO itemVO = convertVOFromModel(itemModel);
        return CommonReturnType.creatSuccess(itemVO);
    }

    @GetMapping(value = "/listItem")
    public @ResponseBody CommonReturnType listItem(){
        List<ItemModel> itemModelList = itemService.listItem();
        List<ItemVO> itemVOList = itemModelList.stream().map(itemModel -> {
            ItemVO itemVO = this.convertVOFromModel(itemModel);
            return itemVO;
        }).collect(Collectors.toList());
        return CommonReturnType.creatSuccess(itemVOList);
    }


    private ItemVO convertVOFromModel(ItemModel itemModel){
        if(itemModel == null){
            return null;
        }
        ItemVO itemVO = new ItemVO();
        BeanUtils.copyProperties(itemModel, itemVO);
        if(itemModel.getPromoModel() != null){
            PromoModel promoModel = itemModel.getPromoModel();
            itemVO.setPromoStatus(promoModel.getStatus());
            itemVO.setPromoId(promoModel.getId());
            itemVO.setStartDate(promoModel.getStartDate().toString(DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")));
            itemVO.setPromoPrice(promoModel.getPromoItemPrice());
        } else {
            itemVO.setPromoStatus(0);
        }
        return itemVO;
    }
}
